package com.crm.system.service.impl.knowledgeitem;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.common.util.DateUtil;
import com.crm.model.entity.knowledgeitem.KnowledgeSection;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.knowledgeitem.KnowledgeSectionQO;
import com.crm.model.vo.knowledgeitem.KnowledgeSectionVO;
import com.crm.service.knowledgeitem.KnowledgeSectionDbService;
import com.crm.system.service.knowledgeitem.KnowledgeSectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 知识章节
 * @Author lixq-f
 * @Date 2021/5/7
 * @Version V1.0
 **/
@Service
public class KnowledgeSectionServiceImpl implements KnowledgeSectionService {


    @Resource
    private KnowledgeSectionDbService sectionDbService;

    @Override
    public Boolean insert(KnowledgeSectionQO sectionQO) {
        KnowledgeSection section = new KnowledgeSection();
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        BeanUtils.copyProperties(sectionQO, section);
        section.setSectionId(CommonUtils.getGUID());
        section.setCreateTm(DateUtil.getSysDate());
        section.setCreatePeopleId(userAccount.getAccountId());
        section.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        return sectionDbService.insert(section);
    }

    @Override
    public Boolean update(KnowledgeSectionQO sectionQO) {
        KnowledgeSection section = new KnowledgeSection();
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        BeanUtils.copyProperties(sectionQO, section);
        section.setUpdateTm(DateUtil.getSysDate());
        section.setUpdatePeopleId(userAccount.getAccountId());
        return sectionDbService.update(section);
    }

    @Override
    public DhPageInfo<KnowledgeSectionVO> selectListByKnItemId(String itemId,
                                                               Integer pageIndex,
                                                               Integer pageSize) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<KnowledgeSectionVO> sectionList = sectionDbService.selectListByKnItemId(itemId);
        PageInfo<KnowledgeSectionVO> pageInfo = new PageInfo<>(sectionList);
        return new DhPageInfo<KnowledgeSectionVO>(pageInfo, sectionList);
    }

    @Override
    public Boolean deleteById(String sectionId, short deleteStatus) {
        return sectionDbService.deleteById(sectionId, deleteStatus);
    }


    @Override
    public KnowledgeSection selectKnSectionByKey(String sectionId) {
        KnowledgeSection section = sectionDbService.selectKnSectionByKey(sectionId);
        if (Objects.isNull(section))
            return new KnowledgeSection();
        return section;
    }


}
